﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using System.Web.Security;
using Project3.Models;
using CodeFirstMembershipSharp;
using Project3.XML;

namespace Project3
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //DataContext users = new DataContext();
            //users.Database.Initialize(true);

            Database.SetInitializer<DataContext>(new DataContextInitializer());

            MembershipConfiguration config = new MembershipConfiguration()
            {
                Roles = new List<string>()
                {
                    "Admin", "Moderator", "Organizer", "Member"                      
                },
                Users = new List<SimpleUser>()
                {
                    new SimpleUser() { Username = "admin", Password = "admin", Email = "admin@demo.com" },
                    new SimpleUser() { Username = "mike", Password = "mike", Email = "moderator@demo.com" },
                    new SimpleUser() { Username = "odin", Password = "odin", Email = "organizer1@demo.com" },
                    new SimpleUser() { Username = "olaf", Password = "olaf", Email = "organizer2@demo.com" },
                    new SimpleUser() { Username = "omar", Password = "omar", Email = "organizer3@demo.com" },
                    new SimpleUser() { Username = "abby", Password = "abby", Email = "member1@demo.com" },
                    new SimpleUser() { Username = "burt", Password = "burt", Email = "member2@demo.com" },
                    new SimpleUser() { Username = "carl", Password = "carl", Email = "member3@demo.com" },
                    new SimpleUser() { Username = "doug", Password = "doug", Email = "member4@demo.com" },
                    new SimpleUser() { Username = "eric", Password = "eric", Email = "member5@demo.com" },
                    new SimpleUser() { Username = "finn", Password = "finn", Email = "member6@demo.com" },
                    new SimpleUser() { Username = "gabe", Password = "gabe", Email = "member7@demo.com" },
                    new SimpleUser() { Username = "hugo", Password = "hugo", Email = "member8@demo.com" },
                    new SimpleUser() { Username = "ivan", Password = "ivan", Email = "member9@demo.com" }
                },
                UsersInRoles = new List<UserInRole>()
                {
                    new UserInRole() { Username = "admin", Role = "Admin" },
                    new UserInRole() { Username = "mike", Role = "Moderator" },
                    new UserInRole() { Username = "odin", Role = "Organizer" },
                    new UserInRole() { Username = "olaf", Role = "Organizer" },
                    new UserInRole() { Username = "omar", Role = "Organizer" },
                    new UserInRole() { Username = "abby", Role = "Member" },
                    new UserInRole() { Username = "burt", Role = "Member" },
                    new UserInRole() { Username = "carl", Role = "Member" },
                    new UserInRole() { Username = "doug", Role = "Member" },
                    new UserInRole() { Username = "eric", Role = "Member" },
                    new UserInRole() { Username = "finn", Role = "Member" },
                    new UserInRole() { Username = "gabe", Role = "Member" },
                    new UserInRole() { Username = "hugo", Role = "Member" },
                    new UserInRole() { Username = "ivan", Role = "Member" }
                }
            };
            XMLSerializer.SerializeToXML(config);
        }
    }
}